<?php
/**
 * PromotionController for managing promotions
 *
 * @author fusq
 */
require_once BaseUtil::getModulePath("base") . '/controllers/BaseController.php';
require_once BaseUtil::getModulePath("promotion")  .  '/forms/ShippingForm.php';
require_once BaseUtil::getModulePath("promotion")  .  '/forms/CouponForm.php';
require_once BaseUtil::getModulePath("promotion")  .  '/forms/CouponPubForm.php';
require_once BaseUtil::getModulePath("promotion")  .  '/forms/SalePriceForm.php';
require_once BaseUtil::getModulePath("promotion")  .  '/forms/EditSalePriceForm.php';
require_once BaseUtil::getModulePath("promotion")  .  '/services/PromotionService.php';
require_once BaseUtil::getModulePath("promotion")  .  '/constants/PromotionConstant.php';
require_once BaseUtil::getModulePath("inventory")  .  '/services/CategoryService.php';
require_once BaseUtil::getModulePath("customer")  .  '/constants/CustomerConstant.php';
require_once BaseUtil::getModulePath ( "component" ) . '/Number.php';
require_once BaseUtil::getModulePath("promotion") . "/forms/SpecialForm.php";
require_once BaseUtil::getModulePath("promotion") . "/forms/SpecialNameForm.php";
require_once BaseUtil::getModulePath("promotion") . "/forms/EditSpecialNameForm.php";

class Promotion_PromotionController extends BaseController {


    public function init() {
        parent::init();
        $this->view->script = array('lib/ui.dialog.js','promotion.js');
        $this->addLocaleData("order");
        $this->addLocaleData('customer');
    }

    public function shippingAction () {
        $shippingFrom = new ShippingForm();
        $promotionService = new PromotionService();
        $promotion = $promotionService->getFreeShippingPromotion();
        $shippingFrom->getElement("name")->setValue($promotion["name"]);
        $shippingFrom->getElement("status")->setValue($promotion["status"]);
        $shippingFrom->getElement("total")->setValue($promotion["condition_value"]);
        $shippingFrom->getElement("description")->setValue($promotion["short_description"]);
        $shippingFrom->getElement("shippingMethod")->setValue($promotion["condition_second_value"]);
        $this->view->form = $shippingFrom;
    }

    public function subshippingAction() {
        $shippingFrom = new ShippingForm();
        $promotionService = new PromotionService();
        if (!$shippingFrom->isValid($_POST)) {
            $this->view->form = $shippingFrom;
            return $this->render('shipping');
        }
        $promotion = array();
        $condition = array();
        $name = $shippingFrom->getValue("name");
        $status = $shippingFrom->getValue("status");
        $total = 0;
        $shippingMethod = 1;
        $description = "";
        if($status == 1) {
            $total = $shippingFrom->getValue("total");
            $shippingMethod = $shippingFrom->getValue("shippingMethod");
            $description = $shippingFrom->getValue("description");
        }
        $promotion["name"] = $name;
        $promotion["status"] = $status;
        $promotion["short_description"] = $description;
        $condition["condition_value"] = $total;
        $condition["condition_second_value"] = $shippingMethod;
        $promotionService->updateFreeShippingPromotion($promotion,$condition);
        $this->_redirect('/promotion/promotion/shipping');
    }

    public function couponAction() {
        $promotionService = new PromotionService();
        $this->view->dbGrid = $promotionService->getCouponPromotion($this->view, $_POST);
    }

    public function coupontypeAction() {
        $couponForm = new CouponForm();
        $this->view->form = $couponForm;
    }

    public function suncoupontypeAction() {
        $promotiontype = $_POST['type'];
        $form = new CouponPubForm();
        $form->getElement("priority")->setValue(0);
        if($promotiontype == 2) {
            $Restrictiontype = PromotionConstant::getRestrictionType(array(1,2,6,8,9));
            $actiontype = PromotionConstant::getActionType(array(1,2,3));
            $categoryService = new CategoryService();
            $categoriesXml = $categoryService->getAllCategoriesXml();
            $time = PromotionConstant::getTimeList();
            $this->view->categoryXml = $categoriesXml;
            $this->view->Restrictiontype = $Restrictiontype;
            $this->view->actiontype = $actiontype;
            $this->view->form = $form;
            $this->view->time = $time;
            $this->view->promotionttype = $promotiontype;
            $this->view->script = array('promotion.js','lib/ui.dialog.js','lib/dhtmlXCommon.js','lib/dhtmlXTree.js','lib/dhtmlXTreeExtend.js','inventory.js');
            return $this->render('productcoupon');
        }else if($promotiontype == 3) {
            $Restrictiontype = PromotionConstant::getRestrictionType(array(3,6,8,9));
            $actiontype = PromotionConstant::getActionType(array(1,2,3));
            $time = PromotionConstant::getTimeList();
            $operator = PromotionConstant::getSubroralOperatorList();
            $this->view->Restrictiontype = $Restrictiontype;
            $this->view->actiontype = $actiontype;
            $this->view->form = $form;
            $this->view->time = $time;
            $this->view->promotionttype = $promotiontype;
            $this->view->operator =$operator;
            return $this->render('ordercoupon');
        }else if($promotiontype == 4) {
            $Restrictiontype = PromotionConstant::getRestrictionType(array(6,8,9));
            $actiontype = PromotionConstant::getActionType(array(2,3));
            $time = PromotionConstant::getTimeList();
            $operator = PromotionConstant::getSubroralOperatorList();
            $this->view->Restrictiontype = $Restrictiontype;
            $this->view->actiontype = $actiontype;
            $this->view->form = $form;
            $this->view->time = $time;
            $this->view->promotionttype = $promotiontype;
            return $this->render('salecoupon');
        }
    }
    public function subproductcouponAction() {
    	$form = new CouponPubForm();
        if (!$form->isValid($_POST)) {
            $Restrictiontype = PromotionConstant::getRestrictionType(array(1,2,6,8,9));
            $actiontype = PromotionConstant::getActionType(array(1,2,3));
            $categoryService = new CategoryService();
            $categoriesXml = $categoryService->getAllCategoriesXml();
            $time = PromotionConstant::getTimeList();
            $this->view->categoryXml = $categoriesXml;
            $this->view->Restrictiontype = $Restrictiontype;
            $this->view->actiontype = $actiontype;
            $this->view->form = $form;
            $this->view->time = $time;
            $this->view->promotionttype = HttpUtil::getPostString('promotiontype');
            $this->view->script = array('promotion.js','lib/ui.dialog.js','lib/dhtmlXCommon.js','lib/dhtmlXTree.js','lib/dhtmlXTreeExtend.js','inventory.js');
            return $this->render('productcoupon');
        }
      
        $promotionService = new PromotionService();
        $promotionService->saveProductPromotion($form,$_POST);
        $this->_redirector->gotoUrl('promotion/promotion/coupon');
    }


    public function subordercouponAction() {
        $form = new CouponPubForm();
        if (!$form->isValid($_POST)) {
            $Restrictiontype = PromotionConstant::getRestrictionType(array(3,6,8,9));
            $actiontype = PromotionConstant::getActionType(array(1,2,3));
            $time = PromotionConstant::getTimeList();
            $operator = PromotionConstant::getSubroralOperatorList();
            $this->view->Restrictiontype = $Restrictiontype;
            $this->view->actiontype = $actiontype;
            $this->view->time = $time;
            $this->view->promotionttype = HttpUtil::getPostString('promotiontype');
            $this->view->operator =$operator;
            $this->view->form = $form;
            return $this->render('ordercoupon');
        }
        $promotionService = new PromotionService();
        $promotionService->saveOrderPromotion($form,$_POST);
        $this->_redirector->gotoUrl('promotion/promotion/coupon');
    }

    public function subsalecouponAction() {
        $form = new CouponPubForm();
        if (!$form->isValid($_POST)) {
            $Restrictiontype = PromotionConstant::getRestrictionType(array(6,8,9));
            $actiontype = PromotionConstant::getActionType(array(2,3));
            $time = PromotionConstant::getTimeList();
            $operator = PromotionConstant::getSubroralOperatorList();
            $this->view->Restrictiontype = $Restrictiontype;
            $this->view->actiontype = $actiontype;
            $this->view->form = $form;
            $this->view->time = $time;
            $this->view->promotionttype = HttpUtil::getPostString('promotiontype');
            $this->view->operator =$operator;
            return $this->render('salecoupon');
        }
        $promotionService = new PromotionService();
        $promotionService->saveSalePromotion($form,$_POST);
        $this->_redirector->gotoUrl('promotion/promotion/coupon');
    }

    public function editcouponAction() {
        $promotionid = $this->_getParam('id');
        $promotionService = new PromotionService();
        $promotion = $promotionService->getPromotionByid($promotionid);
        $promotiontype =$promotion['promotion_type'];
        $this->view->promotion = $promotion;
        $form = new CouponPubForm();
        $form->getElement("name")->setValue($promotion["name"]);
        $form->getElement("show_in_product")->setValue($promotion["show_in_product"]);
        $form->getElement("short_description")->setValue($promotion["short_description"]);
        $form->getElement("long_description")->setValue($promotion["long_description"]);
        //$form->getElement("code")->setValue($promotion["code"]);
        $form->getElement("status")->setValue($promotion["status"]);
        $form->getElement("auto_apply")->setValue($promotion["auto_apply"]);
        $form->getElement("uses_per_customer")->setValue($promotion["uses_per_customer"]);
        $form->getElement("uses")->setValue($promotion["total_uses"]);
        $form->getElement("stackable")->setValue($promotion["stackable"]);
        $form->getElement("priority")->setValue($promotion["priority"]);
        //get Code For Code Restriction
        $codeConditions = $promotionService->getConditionByType($promotionid,7);
        
        if(!empty($codeConditions)){
        	$codeArr = array();
        	foreach($codeConditions as $codeCondition){
        		$codeInfo = array();
        		$customerId = intval($codeCondition['condition_value']);
        		$codeInfo[0] = $codeCondition['code_value'];
        		$codeInfo[1] = $customerId>0?$customerId:'';
        		array_push($codeArr,$codeInfo);
        	}
           
            $this->view->codes = $codeArr;
            $this->view->showCodeRes = 1;
        }
        
       //get customers
         $customers = array();
         $customerIds = array();
         $customerConditions = $promotionService->getConditionByType($promotionid,6);
         if(!empty($customerConditions)){
              foreach($customerConditions as $customerCondition){
                  $customerIds[] = intval($customerCondition['condition_value']);
              }
              $customers = $promotionService->getCustomersByIds($customerIds);
         }
         $this->view->customers = $customers;
       //get certf condition  
         $certfConditions = $promotionService->getConditionByType($promotionid,8);
         $certfArr = array();
         if(!empty($certfConditions)){
             foreach($certfConditions as $certfCondition){
             	array_push($certfArr,intval($certfCondition['condition_value']));
             }
         }
         $this->view->certfArr = $certfArr;
        //get rank condition
         $rankConditions = $promotionService->getConditionByType($promotionid,9);
         $rankArr = array();
         if(!empty($rankConditions)){
             foreach($rankConditions as $rankCondition){
             	array_push($rankArr,intval($rankCondition['condition_value']));
             }
         }
        $this->view->rankArr = $rankArr;
       
        if($promotiontype==2) {
            $Restrictiontype = PromotionConstant::getRestrictionType(array(1,2,6,8,9));
            $actiontype = PromotionConstant::getActionType(array(1,2,3));
            //get category
            $categoryService = new CategoryService();
            $categoriesXml = $categoryService->getAllCategoriesXml();          
            $category = $promotionService->getConditionByType($promotionid,2);
            $action = $promotionService->getActionBypromotionid($promotionid);
            
            //get productItem
            $productItems = array();
            $productItemIds = array();
            $itemConditions = $promotionService->getConditionByType($promotionid,1);
            if(!empty($itemConditions)){
                foreach($itemConditions as $itemCondition){
                    $productItemIds[] = intval($itemCondition['condition_value']);
                }
                $productItems = $promotionService->getProductItemByIds($productItemIds);
            }
            
            
          
            $time = PromotionConstant::getTimeList();
            $this->view->categoryXml = $categoriesXml;
            $this->view->Restrictiontype = $Restrictiontype;
            $this->view->actiontype = $actiontype;
            $this->view->form = $form;
            $this->view->time = $time;
            $this->view->promotionid = $promotionid;
            $this->view->script = array('promotion.js','lib/ui.dialog.js','lib/dhtmlXCommon.js','lib/dhtmlXTree.js','lib/dhtmlXTreeExtend.js','inventory.js');
            $this->view->from = strtotime($promotion['date_from']);
            $this->view->to = strtotime($promotion['date_to']);
            $this->view->category = $category;
            $this->view->productItems = $productItems;
            $this->view->action = $action;
            return $this->render('editproductcoupon');
        }elseif($promotiontype==3) {
        	$form->getElement("exhaust_point")->setValue($promotion["exhaust_points"]);
        	$form->getElement("min_ac_point")->setValue($promotion["min_ac_points"]);
        	$form->getElement("start_date")->setValue(substr($promotion["exchange_start_date"],0,10));
        	$form->getElement("end_date")->setValue(substr($promotion["exchange_end_date"],0,10));
        	$this->view->isExchangePoint = $promotion['is_exchange_point'];
            $Restrictiontype = PromotionConstant::getRestrictionType(array(3,6,8,9));
            $actiontype = PromotionConstant::getActionType(array(1,2,3));
            $operator = PromotionConstant::getSubroralOperatorList();
            $time = PromotionConstant::getTimeList();
            $condition = $promotionService->getConditionByType($promotionid,3);
            $action = $promotionService->getActionBypromotionid($promotionid);
            $this->view->Restrictiontype = $Restrictiontype;
            $this->view->actiontype = $actiontype;
            $this->view->form = $form;
            $this->view->time = $time;
            $this->view->promotionid = $promotionid;
            $this->view->from = strtotime($promotion['date_from']);
            $this->view->to = strtotime($promotion['date_to']);
            $this->view->operator =$operator;
            $this->view->action = $action;
            $this->view->condition = $condition;
            return $this->render('editordercoupon');
        }elseif($promotiontype==4) {
        	$Restrictiontype = PromotionConstant::getRestrictionType(array(6,8,9));
            $actiontype = PromotionConstant::getActionType(array(2,3));
            $time = PromotionConstant::getTimeList();
            $action = $promotionService->getActionBypromotionid($promotionid);
            $this->view->Restrictiontype = $Restrictiontype;
            $this->view->actiontype = $actiontype;
            $this->view->form = $form;
            $this->view->time = $time;
            $this->view->promotionid = $promotionid;
            $this->view->from = strtotime($promotion['date_from']);
            $this->view->to = strtotime($promotion['date_to']);
            $this->view->action = $action;
            return $this->render('editsalecoupon');
        }
    }

    public function subeditsalecouponAction() {
        $form = new CouponPubForm();
        $promotionService = new PromotionService();
        $promotionid = $_POST['promotionid'];
        if (!$form->isValid($_POST)) {
            $promotion = $promotionService->getPromotionByid($promotionid);
            $Restrictiontype = PromotionConstant::getRestrictionType(array(6,8,9));
            $actiontype = PromotionConstant::getActionType(array(1,2,3));
            $action = $promotionService->getActionBypromotionid($promotionid);
            $time = PromotionConstant::getTimeList();
            $this->view->Restrictiontype = $Restrictiontype;
            $this->view->actiontype = $actiontype;
            $this->view->form = $form;
            $this->view->time = $time;
            $this->view->promotionid = $promotionid;
            $this->view->from = strtotime($promotion['date_from']);
            $this->view->to = strtotime($promotion['date_to']);
            $this->view->action = $action;
            $this->view->promotion =$promotion;
            return $this->render('editsalecoupon');
        }
        $promotionService->updateSalePromotion($form, $_POST);
        $this->_redirector->gotoUrl('promotion/promotion/coupon');
    }

    public function subeditordercouponAction() {
        $form = new CouponPubForm();
        $promotionService = new PromotionService();
        $promotionid = $_POST['promotionid'];
        if (!$form->isValid($_POST)) {
            $promotion = $promotionService->getPromotionByid($promotionid);
            $condition = $promotionService->getConditionByType($promotionid,3);
            $Restrictiontype = PromotionConstant::getRestrictionType(array(3,6,8,9));
            $actiontype = PromotionConstant::getActionType(array(1,2,3));
            $action = $promotionService->getActionBypromotionid($promotionid);
            $time = PromotionConstant::getTimeList();
            $this->view->Restrictiontype = $Restrictiontype;
            $this->view->actiontype = $actiontype;
            $this->view->form = $form;
            $this->view->time = $time;
            $this->view->promotionid = $promotionid;
            $this->view->from = strtotime($promotion['date_from']);
            $this->view->to = strtotime($promotion['date_to']);
            $this->view->action = $action;
            $this->view->condition = $condition;
            $this->view->promotion =$promotion;
            return $this->render('editordercoupon');
        }
        $promotionService->updateOrderPromotion($form, $_POST);
        $this->_redirector->gotoUrl('promotion/promotion/coupon');
    }

    public function subeditproductcouponAction() {
    	
        $form = new CouponPubForm();
        $promotionService = new PromotionService();
        $promotionid = $_POST['promotionid'];
        if (!$form->isValid($_POST)) {
            $promotion = $promotionService->getPromotionByid($promotionid);
            $category = $promotionService->getConditionByType($promotionid,2);
            $action = $promotionService->getActionBypromotionid($promotionid);
            $Restrictiontype = PromotionConstant::getRestrictionType(array(1,2,6,8,9));
            $actiontype = PromotionConstant::getActionType(array(1,2,3));
            $categoryService = new CategoryService();
            $categoriesXml = $categoryService->getAllCategoriesXml();
            $time = PromotionConstant::getTimeList();
            $this->view->categoryXml = $categoriesXml;
            $this->view->Restrictiontype = $Restrictiontype;
            $this->view->actiontype = $actiontype;
            $this->view->form = $form;
            $this->view->time = $time;
            $this->view->promotionid = $promotionid;
            $this->view->script = array('promotion.js','lib/ui.dialog.js','lib/dhtmlXCommon.js','lib/dhtmlXTree.js','lib/dhtmlXTreeExtend.js','inventory.js');
            $this->view->category = $category;
            $this->view->action = $action;
            $this->view->promotion =$promotion;
            return $this->render('editproductcoupon');
        }
        $promotionService->updateProductPromotion($form, $_POST);
        $this->_redirector->gotoUrl('promotion/promotion/coupon');
    }
    
    public function deleteAction() {
        $id = $this->_getParam('id');
        $promotionService = new PromotionService();
        $promotionService->deletePromotion($id);
        $this->_redirector->gotoUrl('promotion/promotion/coupon');
    }
    public function validationsameAction() {
        $name = mysql_escape_string(strtolower(HttpUtil::getPostString ('name')));
        $id = HttpUtil::getPostString ('id');
        $type = HttpUtil::getPostString ('type');
        $result = false;
        $promotionService = new PromotionService();
        if($type==1) {
            $promotion =$promotionService->getPromotionByName($name);
            if(!empty($promotion)) {
                if($promotion['id']!=$id) {
                    $result = true;
                }
            }
        }else if($type==2) {
                $promotion =$promotionService->getPromotionByCode($name);
                if(!empty($promotion)) {
                    if($promotion['id']!=$id) {
                        $result = true;
                    }
                }
            }
        echo json_encode ($result);exit;
    }

    public function addsalepriceAction() {
        $form = new SalePriceForm();
        $this->view->form = $form;
    }

    public function subsalepriceAction() {
        $form = new SalePriceForm();
        if (!$form->isValid($_POST)) {
            $this->view->form = $form;
            return $this->render('addsaleprice');
        }
        $promotionService = new PromotionService();
        $promotionService->addSalePrice($form,$_POST);
        $this->_redirector->gotoUrl('promotion/promotion/saleprice');
    }

    public function salepriceAction() {
        $promotionService = new PromotionService();
        $this->view->dbGrid =  $promotionService->getSalePriceGrid($this->view, $_POST);
    }
  
    public function editsalepriceAction() {
        $id = $this->_getParam("id");
        $form = new EditSalePriceForm();
        $promotionService = new PromotionService();
        $salePricePromotion = $promotionService->getSalePricePromotionById($id);
        $productItemId = $salePricePromotion['product_item_id'];
        $productItemInfo = $promotionService->getProductItemInfo($productItemId);
        $form->getElement("salePrice")->setValue($salePricePromotion['sale_price']);
        $form->getElement("from")->setValue(date("Y-m-d",strtotime($salePricePromotion['date_from'])));
        $form->getElement("to")->setValue(date("Y-m-d",strtotime($salePricePromotion['date_to'])));
        $this->view->productItem = $productItemInfo;
        $this->view->form = $form;
        $this->view->id = $salePricePromotion['id'];
    }

    public function subeditsalepriceAction() {
        $form = new EditSalePriceForm();
        $promotionService = new PromotionService();
        $id = trim($_POST["id"]);
        if (!$form->isValid($_POST)) {
            $salePricePromotion = $promotionService->getSalePricePromotionById($id);
            $productItemId = $salePricePromotion['product_item_id'];
            $productItemInfo = $promotionService->getProductItemInfo($productItemId);
            $this->view->form = $form;
            $this->view->productItem = $productItemInfo;
            $this->view->id = $salePricePromotion['id'];
            return $this->render('editsaleprice');
        }
        $data = array();
        $data['sale_price'] = $form->getValue("salePrice");
        $data['date_from'] = $form->getValue("from");
        $data['date_to'] = $form->getValue("to");
        $promotionService->updateSalePricePromotion($data, $id);
        $this->_redirector->gotoUrl('promotion/promotion/saleprice');
    }

    public function deletesalepriceAction() {
        $id = $this->_getParam("id");
        if(!empty($id)) {
            $promotionService = new PromotionService();
            $promotionService->deleteSalePricePromotion($id);
        }
        $this->_redirector->gotoUrl('promotion/promotion/saleprice');
    }
    
    public function checkcodeuniqueAction(){
    	$checkCode = HttpUtil::getPostString('code');
    	$promotionId = HttpUtil::getPostString('promotion_id');
    	$promotionService = new PromotionService();
    	$codeConditions = $promotionService->getAllConditionByType(7,$promotionId);
    	$codeArr = array();
    	$result = 0;
    	
    	foreach($codeConditions as $codeCondition){
    		$code = trim($codeCondition['code_value']);
    		if($checkCode == $code){
    			$result=1;
    			echo $result;
    			exit();
    		}
    	}
    	
    	echo $result;
    	exit();
    }
    
    public function getcouponcodeAction(){
    	
        $promotionService = new PromotionService();
    	$codeConditions = $promotionService->getAllConditionByType(7);
    	$codeArr = array();
    	$result = 0;
    	while(!$result){
    		$couponCode = Number::getGiftCouponCode();
    		$result=1;
    		if(!empty($codeConditions)){
    	        foreach($codeConditions as $codeCondition){
    		        $code = trim($codeCondition['code_value']);
    		        if($couponCode == $code){
    			        $result=0;
    			        break;
    		        }
    	        }
    		}
    	}
    	
    	echo json_encode($couponCode);
    	exit;
    }

    //促销专场名称设置 @author dmgy
    public function specialnameAction()
    {
        $promotionService = new PromotionService();
        $this->view->dbGrid =  $promotionService->getSpecialNamePromotion($this->view, $_POST);
    }    

    public function addspecialnameAction() {
        $form = new SpecialNameForm("subspecialname");
        $this->view->form = $form;
    }      

    public function subspecialnameAction() {
        $form = new SpecialNameForm("addspecialname");
        if (!$form->isValid($_POST)) {
            $this->view->form = $form;
            return $this->render('addspecialname');
        }
        $promotionService = new PromotionService();
        $promotionService->addSpecialName($form,$_POST); 
        $this->_redirector->gotoUrl('promotion/promotion/specialname');
    }       

    public function editspecialnameAction() {
        $id = $this->_getParam("id");
        $form = new SpecialNameForm("subeditspecialname");
        $promotionService = new PromotionService();
        $specialPromotion = $promotionService->getSpecialNamePromotionById($id);
        $form->getElement("specialName")->setValue($specialPromotion['title']);
        $this->view->form = $form;
        $this->view->id = $specialPromotion['id'];
    }    

    public function subeditspecialnameAction() {
        $form = new SpecialNameForm("subeditspecialname");
        $promotionService = new PromotionService();
        $id = trim($_POST["id"]);
        if (!$form->isValid($_POST)) {
            $specialPromotion = $promotionService->getSpecialNamePromotionById($id);
            $this->view->form = $form;
            $this->view->id = $specialPromotion['id'];
            return $this->render('editspecialname');
        }
        if(!$id)
        {
        	return $this->render('editspecialname');
        }
        $data = array();
        $data['title'] = $form->getValue("specialName");
        $data['modified'] = date ( "Y-m-d H:i:s" );
        $promotionService->updateSpecialNamePromotion($data, $id);
        $this->_redirector->gotoUrl('promotion/promotion/specialname');
    }     

    public function deletespecialnameAction() {
        $id = $this->_getParam("id");
        if(!empty($id)) {
            $promotionService = new PromotionService();
            $promotionService->deleteSpecialNamePromotion($id);
        }
        $this->_redirector->gotoUrl('promotion/promotion/specialname');
    }      
    
    
    //促销专场设置  author dmgy
    public function specialAction()
    {
        $promotionService = new PromotionService();
        $this->view->dbGrid =  $promotionService->getTextPromotion($this->view, $_POST);
    }
    
    public function addspecialAction() {
        $form = new SpecialForm("subspecial");
        $this->view->form = $form;
    }    

    public function subspecialAction() {
        $form = new SpecialForm("addspecial");
        if (!$form->isValid($_POST)) {
            $this->view->form = $form;
            return $this->render('addspecial');
        }
        $promotionService = new PromotionService();
        $promotionService->addSpecial($form,$_POST); 
        $this->_redirector->gotoUrl('promotion/promotion/special');
    }    

    public function editspecialAction() {
        $id = $this->_getParam("id");
        $promotionService = new PromotionService();
        $specialPromotion = $promotionService->getSpecialPromotionById($id);
        $form = new SpecialForm("subeditspecial",$specialPromotion);
        $this->view->form = $form;
        $this->view->id = $specialPromotion['id'];
    } 

    public function subeditspecialAction() {
        $form = new SpecialForm("subeditspecial");
        $promotionService = new PromotionService();
        $id = trim($_POST["id"]);
        if (!$form->isValid($_POST)) {
            $specialPromotion = $promotionService->getSpecialPromotionById($id);
            $this->view->form = $form;
            $this->view->id = $specialPromotion['id'];
            return $this->render('editspecial');
        }
        if(!$id)
        {
        	return $this->render('editspecial');
        }
        $data = array();
        $data['product_item_id'] = $form->getValue("productCode");
        $data['special_type'] = $form->getValue("specialCode");
        $data['special_sale_id'] = $form->getValue("special_sale_id");
        $data['modified'] = date ( 'Y-m-d H:i:s' );
        $promotionService->updateSpecialPromotion($data, $id);
        $this->_redirector->gotoUrl('promotion/promotion/special');
    }    

    public function deletespecialAction() {
        $id = $this->_getParam("id");
        if(!empty($id)) {
            $promotionService = new PromotionService();
            $promotionService->deleteSpecialPromotion($id);
        }
        $this->_redirector->gotoUrl('promotion/promotion/special');
    }    
         
}

